<?php

/**
 * Created by PhpStorm.
 * User: CCA
 * Date: 2019/11/21
 * Time: 11:15
 */

namespace app\service;

use app\common\model\Waybill;

class GoodsService
{
    protected static $obj = null;

    public static function getInstance()
    {
        if (self::$obj instanceof GoodsService) {
            return self::$obj;
        } else {
            self::$obj = new GoodsService();
            return self::$obj;
        }
    }

    public function calculatePrice($supplier_type, Array $goods)
    {
        /*switch ($supplier_type){
            case 1:
                if($goods['g_type']==1){
                    $price = $goods['goods_price']+(($goods['goods_price']+$goods['g_value']*0.01))+$goods['markup_price'];
                }else{
                    $price = $goods['goods_price']*$goods['g_value']+$goods['markup_price'];
                }
                break;
            case 2:
                if($goods['t_type']==1){
                    $price = $goods['goods_price']+(($goods['goods_price']*$goods['t_value']*0.01));
                }else{
                    $price = $goods['goods_price']*$goods['t_value'];
                }
                break;
            default:
                $price = $goods['goods_price'];
        }
        return $price;
   */
        #12.26 修改
        $store_id = get_parent_store_id();
        if ($supplier_type == 1) {
            #加价
            $y_goods_info = db('y_goods')->where('store_id', $store_id)->where('goods_id', $goods['goods_id'])->find();
            //p($y_goods_info);die;
            $price = (float)$goods['gu_final_price'] + (float)$y_goods_info['price'];
        } else {
            #按照提成走
            $price = (float)$goods['ti_final_price'];
        }
        return $price;
    }

    public function calculateFreight(Array $goods_waybill, $area_id, $type_array)
    {
        $wayBill = new Waybill();

        $store_freight_info = [];
        $info = [];
        $freight_info = [];
        //首先重新将数组赋值 将所有涉及到的值获取过来
        foreach ($type_array as $k => $v) {   #$k 为店铺id
            //遍历所有商品
            foreach ($v as $kk => $vv) {

                //获取商品的总体积和总重量
                //获取商品详情
                $goods_info = db('goods')->where(['goods_id' => $vv['goods_id']])->find();

                $type_array[$k][$kk]['waybill_id'] = $goods_info['waybill_id'];
                $type_array[$k][$kk]['area_id'] = $area_id;
                $type_array[$k][$kk]['weight'] = $goods_info['weight'];
                $type_array[$k][$kk]['volume'] = $goods_info['volume'];
                $type_array[$k][$kk]['is_w_v'] = $goods_info['is_w_v'];
                $type_array[$k][$kk]['waybill_type'] = $goods_info['waybill_type'];

                $waybill_info = db('waybill')->where(['waybill_id' => $goods_info['waybill_id']])->find();
                $type_array[$k][$kk]['is_bao'] = $waybill_info['is_bao'];
                $type_array[$k][$kk]['f_heavy'] = $waybill_info['f_heavy'];
                $type_array[$k][$kk]['f_heavy_money'] = $waybill_info['f_heavy_money'];
                $type_array[$k][$kk]['c_heavy'] = $waybill_info['c_heavy'];
                $type_array[$k][$kk]['c_heavy_money'] = $waybill_info['c_heavy_money'];
                $type_array[$k][$kk]['area_type'] = $waybill_info['area_type'];
                $type_array[$k][$kk]['area_content'] = $waybill_info['area_content'];
                $type_array[$k][$kk]['f_volume'] = $waybill_info['f_volume'];
                $type_array[$k][$kk]['f_volume_money'] = $waybill_info['f_volume_money'];
                $type_array[$k][$kk]['c_volume'] = $waybill_info['c_volume'];
                $type_array[$k][$kk]['c_volume_money'] = $waybill_info['c_volume_money'];
                $type_array[$k][$kk]['home_price'] = $waybill_info['home_price'];
            }
        }
        foreach ($type_array as $k => $v) {   #$k 为店铺id
            //遍历商品
            foreach ($v as $kk => $vv) {
                if ($vv['is_home'] == 1) {
                    #送货上门
                    if ($vv['waybill_type'] == 3) {
                        #送货上门 不收费用
                        $store_freight_info[$k][$kk]['goods_id'] = $vv['goods_id'];
                        $store_freight_info[$k][$kk]['freight'] = 0;
                    } else {
                        $store_freight_info[$k][$kk]['goods_id'] = $vv['goods_id'];
                        $store_freight_info[$k][$kk]['freight'] = $vv['home_price'];
                    }
                } else {
                    #自提
                    $store_freight_info[$k][$kk]['goods_id'] = $vv['goods_id'];
                    $store_freight_info[$k][$kk]['freight'] = 0;

                }
                if ($vv['is_bao'] == 1) {
                    #包邮商品
                    $store_freight_info[$k][$kk]['freight'] = $store_freight_info[$k][$kk]['freight'] + 0;
                } else {
                    #不是包邮商品
                    if ($vv['is_to_pay'] == 1) {
                        #到付
                        // $store_freight_info[$k][$kk]['freight'] = $store_freight_info[$k][$kk]['freight'] + 0;
                        $store_freight_info[$k][$kk]['freight'] = 0;
                    } else {
                        #不到付
                        #需要组合数组了
                        $key = $vv['waybill_id'] . "_" . $vv['is_w_v'];
                        if (isset($info[$key])) {
                            $info[$key][] = $vv;
                            if ($vv['is_w_v'] == 1) {
                                #抛货
                                $info[$key]['count']['count_volume'] = $info[$key]['count']['count_volume'] + $vv['volume'] * $vv['count'];
                                $info[$key]['count']['home_money'] = $info[$key]['count']['home_money'] + $store_freight_info[$k][$kk]['freight'];
                                $store_freight_info[$k][$kk]['freight'] = 0;
                            } else {
                                #重货
                                $info[$key]['count']['count_weight'] = $info[$key]['count']['count_weight'] + $vv['weight'] * $vv['count'];
                                $info[$key]['count']['home_money'] = $info[$key]['count']['home_money'] + $store_freight_info[$k][$kk]['freight'];
                                $store_freight_info[$k][$kk]['freight'] = 0;
                            }
                        } else {
                            $key = $vv['waybill_id'] . "_" . $vv['is_w_v'];
                            if ($vv['is_w_v'] == 1) {
                                #抛货
                                $info[$key]['count']['count_volume'] = $vv['volume'] * $vv['count'];
                                $info[$key]['count']['count_weight'] = 0;
                                $info[$key]['count']['home_money'] = $store_freight_info[$k][$kk]['freight'];
                                $store_freight_info[$k][$kk]['freight'] = 0;
                            } else {
                                #重货
                                $info[$key]['count']['count_weight'] = $vv['weight'] * $vv['count'];
                                $info[$key]['count']['count_volume'] = 0;
                                $info[$key]['count']['home_money'] = $store_freight_info[$k][$kk]['freight'];
                                $store_freight_info[$k][$kk]['freight'] = 0;
                            }
                            $info[$key][] = $vv;

                        }

                    }

                }

            }
        }
        foreach ($info as $k => $v) {

            foreach ($v as $kk => $vv) {
                if ((string)$kk == 'count') {
                    continue;
                }
                if ($kk > 0) {
                    continue;
                }
                if ($vv['is_w_v'] == 1) {
                    #抛货
                    if (isset($vv['area_content']) && $vv['area_content'] != '') {
                        $area_info = json_decode($vv['area_content'], true);
                    } else {
                        $area_info = false;
                    }

                    $in_area = false;
                    if ($area_info) {
                        foreach ($area_info as $aitem) {
                            if (($area_ids = explode(',', $aitem['area_id'])) && in_array($area_id, $area_ids)) {
                                $in_area = true;
                                if ($v['count']['home_money']) {
                                    $info[$k][$kk]['freight'] = ($price_current = $v['count']['count_volume'] - $vv['f_volume']) <= 0 ?
                                        $aitem['firstunit_area_volume_price'] + $aitem['area_home_price'] :
                                        $aitem['firstunit_area_volume_price'] + $price_current / $vv['c_volume'] * $aitem['continueunit_area_volume_price'] + $aitem['area_home_price'];
                                } else {
                                    $info[$k][$kk]['freight'] = ($price_current = $v['count']['count_volume'] - $vv['f_volume']) <= 0 ?
                                        $aitem['firstunit_area_volume_price'] :
                                        $aitem['firstunit_area_volume_price'] + $price_current / $vv['c_volume'] * $aitem['continueunit_area_volume_price'];
                                }
                                break;
                            }

                        }
                    }
                    if (!$in_area) {
                        $info[$k][$kk]['freight'] = ($price_current = $v['count']['count_volume'] - $vv['f_volume']) <= 0 ?
                            $vv['f_volume_money'] + $v['count']['home_money'] :
                            $vv['f_volume_money'] + $price_current / $vv['c_volume'] * $vv['c_volume_money'] + $v['count']['home_money'];
                    }
                } else {
                    #重货
                    if (isset($vv['area_content']) && $vv['area_content'] != '') {
                        $area_info = json_decode($vv['area_content'], true);
                    } else {
                        $area_info = false;
                    }

                    $in_area = false;
                    if ($area_info) {
                        foreach ($area_info as $aitem) {
                            if (($area_ids = explode(',', $aitem['area_id'])) && in_array($area_id, $area_ids)) {
                                $in_area = true;
                                if ($v['count']['home_money']) {
                                    $info[$k][$kk]['freight'] = ($price_current = $v['count']['count_weight'] - $vv['f_heavy']) <= 0 ?
                                        $aitem['firstunit_area_price'] + $aitem['area_home_price'] :
                                        $aitem['firstunit_area_price'] + $price_current / $vv['c_heavy'] * $aitem['continueunit_area_price'] + $aitem['area_home_price'];
                                } else {
                                    $info[$k][$kk]['freight'] = ($price_current = $v['count']['count_weight'] - $vv['f_heavy']) <= 0 ?
                                        $aitem['firstunit_area_price'] :
                                        $aitem['firstunit_area_price'] + $price_current / $vv['c_heavy'] * $aitem['continueunit_area_price'];

                                }
                                break;
                            }
                        }
                    }
                    if (!$in_area) {
                        $info[$k][$kk]['freight'] = ($price_current = $v['count']['count_weight'] - $vv['f_heavy']) <= 0 ?
                            $vv['f_heavy_money'] + $v['count']['home_money'] :
                            $vv['f_heavy_money'] + $price_current / $vv['c_heavy'] * $vv['c_heavy_money'] + $v['count']['home_money'];
                    }
                }
            }
        }


        $index = 0;
        foreach ($store_freight_info as $k => $v) {
            foreach ($v as $kk => $vv) {
                $type_array[$k][$kk]['freight'] = $vv['freight'];
                $freight_info[$index]['shop_id'] = $k;
                $freight_info[$index]['goods_info'][] = $type_array[$k][$kk];
                // $freight_info[$index]['sum_freight'] = $type_array[$k][$kk];
            }
            $index++;
        }


        if ($info) {
            foreach ($info as $k => $v) {
                foreach ($freight_info as $kk => $vv) {
                    foreach ($vv['goods_info'] as $kkk => $vvv) {
                        if ($v['0']['goods_id'] == $vvv['goods_id']) {
                            $freight_info[$kk]['goods_info'][$kkk]['freight'] = $v['0']['freight'];
                        }


                    }
                }


            }


        }

        foreach ($freight_info as $k => $v) {
            $freight_info[$k]['shop_sum_freight'] = array_sum(array_column($v['goods_info'], 'freight'));
        }
        $freight_info['sum_freight'] = array_sum(array_column($freight_info, 'shop_sum_freight'));

        return $freight_info;
    }


    public static function formatFreight(Array $freights)
    {
        $freight_list = array();
        foreach ($freights as $freight) {
            if (!array_key_exists($freight['store_id'], $freight_list)) {
                $freight_list[$freight['store_id']] = $freight['freight'];
            } else {
                $freight_list[$freight['store_id']] = $freight_list[$freight['store_id']] + $freight['freight'];
            }
        }
        return $freight_list;
    }

}